import allure
import cv2
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from selenium.common import NoSuchElementException


class BasePage:
    _IMPLICIT_WAIT = 5

    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    def find(self, by, locator=None):
        """查找元素"""
        if locator:
            element = self.driver.find_element(by, locator)
        else:
            # by = (By.ID, "xxxx")
            element = self.driver.find_element(*by)

        img_path = self.draw_rectangle_in_screenshot(element)
        allure.attach.file(img_path,name="页面截图",
                           attachment_type=allure.attachment_type.PNG)

        return element

    def get_toast_text(self):
        TOAST_LOC = AppiumBy.XPATH, "//*[@class='android.widget.Toast']"
        return self.driver.find_element(*TOAST_LOC).text

    def draw_rectangle_in_screenshot(self, element, color_rgb=(255, 0, 0)):
        '''在图上上画矩形
        截图 pip install opencv-python
        start_point: 起点的坐标，tuple 类型，例如：(100, 100)
        end_point: 终点的坐标，tuple 类型，例如：(200, 200)
        color_rgb: 画线对应的rgb颜色 例如：(0, 255, 0)
        '''
        start_x = element.location.get("x")
        start_y = element.location.get("y")
        end_x = element.size.get("width") + start_x
        end_y = element.size.get("height") + start_y
        start_point = (start_x, start_y)
        end_point = (end_x, end_y)
        img_path = "tmp.png"
        # self.screenshot(img_path)
        self.driver.save_screenshot(img_path)
        # 读取图片

        image = cv2.imread(img_path)
        # 画矩形
        cv2.rectangle(image, start_point, end_point, color_rgb, 5)
        # 写到文件中
        cv2.imwrite(img_path, image)
        return img_path

    def find_and_click(self, by, locator=None):
        """查找到元素之后完成点击"""
        self.find(by, locator).click()

    def find_and_send(self, text, by, locator=None):
        """查找到元素之后完成点击"""
        self.find(by, locator).send_keys(text)

    def get_text(self, by, locator=None):
        return self.find(by, locator).text

    def swipe_find(self, text, num=6):
        """滑动查找 文字"""
        self.driver.implicitly_wait(1)
        for i in range(num):
            try:
                # 在查找元素的时候， 会激活隐式等待
                element = self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{text}']")
                self.driver.implicitly_wait(self._IMPLICIT_WAIT)
                return element
            except Exception as e:
                print("未找到，滑动")
                # 屏幕尺寸
                size = self.driver.get_window_size()
                # 宽
                width = size.get('width')
                # 高
                height = size.get('height')
                start_x = width / 2
                start_y = height * 0.8
                end_x = start_x
                end_y = height * 0.3
                duration = 2000

                self.driver.swipe(start_x, start_y, end_x, end_y, duration)
        self.driver.implicitly_wait(self._IMPLICIT_WAIT)
        raise NoSuchElementException(f"找了{num}次未找到")
